package com.wyy.controller;

import com.wyy.pojo.Article;
import com.wyy.pojo.Category;
import com.wyy.pojo.PageBean;
import com.wyy.pojo.Result;
import com.wyy.service.ArticleService;
import com.wyy.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增文章
    @PostMapping
    public Result add(@RequestBody @Validated(Article.add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }
    //条件分页列表查询
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    //获取文章详情
    @GetMapping("/detail")
    public Result<Article> detail(@RequestParam Integer id) {
        Article ac = articleService.detail(id);
        return Result.success(ac);
    }

    //更新文章
    @PutMapping
    public Result update(@RequestBody @Validated(Article.update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }
    //删除文章
    @DeleteMapping
    public Result delete (@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }


}
